/**
 * Created by wengca on 2016/12/8.
 */

//var express = require('express');
//var router = express.Router();

var request = require('request');
var multiparty = require('multiparty');
var merge = require('merge');
var uuid = require("node-uuid");
var url = require("url");
var fs = require("fs");
var parseBody = require("../common/buildRequestBody.js").commonParseBody;
/*
//单页（首页，登入之类不需要审核判断的）
app.get('/', function (req, res) {
    var menu = req.params.menu;  //第一级目录
    console.log("进入首页",menu);

    res.render('index');

});*/


//单页（首页，登入之类不需要审核判断的）
app.get('/', function (req, res) {
    var menu = req.params.menu;  //第一级目录
    console.log("进入首页",menu);
    // 不需要pc页面改为进入后台系统
    res.redirect('/boss/login')
    //res.render('index');

});


app.get('/downLoad', function (req, res) {
  var menu = req.params.menu;  //第一级目录
  console.log("下载",menu);
  // 不需要pc页面改为进入后台系统
  res.render('downLoad')
  //res.render('index');
  
});

/**
 * 退出登入
 */
app.get('/logout', function (req, res) {
    console.log("post 获取参数".action);
    var action = "logout";  //第二级目录
    var masterFile = "../control/login.js";
    var masterModele = require(masterFile);

    masterModele[action]({},req,function(){
        res.redirect('/boss/login')
    });
});


/**
 * 登入
 */
app.post('/doLogin', function (req, res) {
    console.log("post 获取参数".action);
    var action = 'doLogin';  //第二级目录
    var masterFile = "../control/login.js";
    var masterModele = require(masterFile);

    getPostInfo(req,res,function(parame){
        masterModele[action](parame,req,function(result){
            res.end(JSON.stringify(result));
        });
    });
});


/**
 * 进入手机新闻页面
 */

app.get('/phone/new1', function (req, res) {
    console.log("手机路径");
    var pathname = req.query;  //pathname => select
    console.log("pathname is:",pathname);
    console.log("url is:",url);
    res.render('phone/new1',{
        id:pathname.id
    },function(err){
        if(err){
            console.log(err)
            res.redirect('/phone/index')
        }else{
            res.render('phone/new1',{
                id:pathname.id
            })
        }
    });

});

/**
 * 手机其他页面
 */
app.get('/phone/:directory', function (req, res) {
    console.log("手机路径");
    var directory = req.params.directory;  //第二级目录
    var url =  "phone/" + directory;
    console.log("url is:",url)
    res.render(url,function(err,result){
        if(err){
            console.log(err)
            res.redirect('/phone/index')
        }else{
            res.render(url)
        }
    });

});


//进入页面(不带参数)
/**
 * pc新闻页面
 */
app.get('/news/news1', function (req, res) {
    var pathname = req.query;  //pathname => select
    var masterFile = "../control/controller.js";
    var masterModele = require(masterFile);

    console.log("pathname is:",pathname);
    console.log("url is:",url);
    res.render('news/news1',{
        id:pathname.id
    });

});

//进入页面(不带参数)
/**
 * 后台页面
 */
app.get('/boss/userModify', function (req, res) {
    console.log("query is:",req.query)
    var masterFile = "../control/controller.js";
    var masterModele = require(masterFile);
    masterModele['userModify'](req.query,req,function(result){
        console.log("result",result);
        var obj = result.info.power;
        var account = result.info.account;
        if(typeof obj == 'string' )
            obj = JSON.parse(obj);
        var showDetail = obj.showTable;
        var showTable = obj.showDetail;
        res.render('boss/userModify',{
            showTable:showTable,
            showDetail:showDetail,
            account:account
        },function(err,info){
            if(err){
                console.log(err)
                res.redirect('/boss/login')
            }else{
                res.render('boss/userModify', {
                    showTable: showTable,
                    showDetail: showDetail,
                    account: account
                })
            }
        });
    });
});
//进入页面(不带参数)
/**
 * 后台页面（老人信息）
 */
app.get('/boss/taccountInfo', function (req, res) {
    console.log("query is:",req.query)
    var masterFile = "../control/hospital.js";
    var masterModele = require(masterFile);
    masterModele['taccountInfo'](req,res,function(){
    });
});


/**
 * 后台页面（员工信息）
 */
app.get('/boss/empoloyeeInfo', function (req, res) {
    console.log("query is:",req.query)
    var masterFile = "../control/hospital.js";
    var masterModele = require(masterFile);
    masterModele['empoloyeeInfo'](req,res,function(){
    });
});


app.get('/boss/AddTaccountDetail', function (req, res) {
    console.log("query is:",req.query)
    var masterFile = "../control/hospital.js";
    var masterModele = require(masterFile);
    masterModele['AddTaccountDetail'](req,res,function(){

    });
});


/**
 * 后台页面
 */
app.get('/boss/:directory', function (req, res) {
    console.log("后台路径");
    var directory = req.params.directory;  //第二级目录
    var url =  "boss/" + directory;
    console.log("url is:",url)
    res.render(url,function(err,result){
        if(err){
            console.log(err)
            res.redirect('/boss/login')
        }else{
            res.render(url)
        }
    });
});

//进入页面(不带参数)
app.get('/:master/:directory', function (req, res) {
    var master = req.params.master;  //第一级目录
    var directory = req.params.directory;  //第二级目录
    var url = master + "/" + directory;
    console.log("两个参数的",url)
    res.render(url);
});


//post路由
/**
 * 上传文件
 */
var multipart = require('connect-multiparty');
app.post('/controller/upload', multipart(), function (req, res) {
    var masterFile = "../control/controller.js";
    var masterModele = require(masterFile);
   // getPostInfo(req,res,function(parame){
        masterModele['upload'](res,req,function(result){
            res.end(JSON.stringify(result));
        });
  //  });

})

/**
 * 上传文件
 */
var multipart = require('connect-multiparty');
app.post('/controller/uploadImage', multipart(), function (req, res) {
    var masterFile = "../control/controller.js";
    var masterModele = require(masterFile);
   // getPostInfo(req,res,function(parame){
        masterModele['uploadImage'](res,req,function(result){
            res.end(JSON.stringify(result));
        });
  //  });

})


//post路由
app.post('/controller/:action', function (req, res) {
    console.log("post 获取参数".action);
    var action = req.params.action;  //第二级目录
    var masterFile = "../control/controller.js";
    var masterModele = require(masterFile);

    getPostInfo(req,res,function(parame){
        masterModele[action](parame,req,function(result){
            res.end(JSON.stringify(result));
        });
    });
});


//post路由
app.post('/hospital/:action',multipart(),  function (req, res) {
    console.log("post 获取参数".action);
    var action = req.params.action;  //第二级目录
    var masterFile = "../control/hospital.js";
    var masterModele = require(masterFile);
   // getPostInfo(req,res,function(parame){
        var parame  = req.body;

        masterModele[action](parame,req,function(result){
            res.end(JSON.stringify(result));
        });
    //});
});

//post路由
app.post('/notice/:action',multipart(),  function (req, res) {
    console.log("post 获取参数".action);
    var action = req.params.action;  //第二级目录
    var masterFile = "../control/node-schedule.js";
    var masterModele = require(masterFile);
   // getPostInfo(req,res,function(parame){
        var parame  = req.body;

        masterModele[action](parame,req,function(result){
            res.end(JSON.stringify(result));
        });
    //});
});


//post路由
app.post('/boss/:action', function (req, res) {
    console.log("post 获取参数".action);
    var action = req.params.action;  //第二级目录
    var masterFile = "../control/server.js";
    var masterModele = require(masterFile);
    console.log("333333333333333333",action,req.body);
    //getPostInfo(req,res,function(parame){
   var  parame = req.body
        console.log("333333333333333333",parame);
        if(parame && parame.sign == 'xiaodao'){
            masterModele[action](req.body,req,function(result){
                res.end(JSON.stringify(result));
            });
        }else {
            res.end(JSON.stringify({code:500,info:"签名错误"}));
        }

   // });
});




//获取参数
var getPostInfo =  function(req,res,cb){
    parseBody(req, function(code, msg, data){
        var str = data;
        console.log("str is",str);
        console.log("code is",code);

		if(code == -1){
			 var err = {code: "500", msg: "请求参数格式错误"}
            res.writeHead(200, {"Content-Type": "text/plain", charset: 'utf-8'});
            err = JSON.stringify(err);
            return res.end(err);
		}
        cb(str)
    });
};

//module.exports = router;